<?php

namespace app\queue\redis;

use app\admin\model\Bot;
use Illuminate\Support\Arr;
use support\Redis;
use Telegram\Bot\Api;
use Throwable;
use Webman\RedisQueue\Consumer;

class SetBotHook implements Consumer
{
    // 要消费的队列名
    public $queue = 'set_bot_hook';

    // 连接名，对应 plugin/webman/redis-queue/redis.php 里的连接`
    public $connection = 'default';

    // 消费
    public function consume($data)
    {
        $bot = (new Bot)->where('id', Arr::get($data, 'id', 0))->first();
        if(!$bot) return ;
        Redis::set('bot_hook_loading:'.$bot->id, 1, 60);
        $telegram = new Api($bot->token);
        dump(getTelegramHookUrl($bot->token));
        $telegram->setWebhook(['url'=>getTelegramHookUrl($bot->token)]);
        if($bot->backups){
            foreach($bot->backups as $backup){
                dump(getTelegramHookUrl($bot->token));
                $te = new Api($backup->token);
                $te->setWebhook(['url'=>getTelegramHookUrl($backup->token)]);             
            }
        }
        Redis::del('bot_hook_loading:'.$bot->id);
    }
            
}
